//
//  LotteryListViewController.m
//  MoXiProject
//
//  Created by 周筱雯 on 2017/7/17.
//  Copyright © 2017年 周筱雯. All rights reserved.
//

#import "LotteryListViewController.h"
#import "LotteryModel.h"
#import "LotteryListCell.h"

#import "LotteryDetailViewController.h"


@interface LotteryListViewController ()
<
    UITableViewDelegate,
    UITableViewDataSource
>

@property (nonatomic,strong) UITableView * tableView;

@property (nonatomic,strong) NSMutableArray * lotterylistArrayList;

@end

@implementation LotteryListViewController


-(UITableView *) tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, DEF_DEVICE_WIDTH, DEF_CONTENT_INTABBAR_HEIGHT) style:UITableViewStylePlain];
        [_tableView setDelegate:self];
        [_tableView setDataSource:self];
        
        [MXUtility setExtraCellLineHidde:_tableView tabHeader:NO tabFooter:NO];
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view addSubview:self.tableView];
    [self.navigationItem setTitle:@"历史开奖"];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    /**
     网络请求
     */
    self.lotterylistArrayList = [[NSMutableArray alloc] initWithCapacity:0];
    [self lotteryListNetWork];
    
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(lotteryListNetWork)];
    [self.tableView.mj_header beginRefreshing];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.lotterylistArrayList count];
}
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * str = @"cell";
    LotteryListCell * cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"LotteryListCell" owner:self options:nil] lastObject];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        
    }
    [cell setlotteryListDataSource:[self.lotterylistArrayList objectAtIndex:indexPath.row] typeName:self.cpName typeImg:self.cpImg];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LotteryListModel * model = [self.lotterylistArrayList objectAtIndex:indexPath.row];
    
    
    LotteryDetailViewController * detail = [[LotteryDetailViewController alloc] init];
    [detail setListModel:model];
    [detail setPrizeModelArrayList:model.prizeArray];
    [detail setCpName:self.cpName];
    [detail setCpImg:self.cpImg];
    
    [self.navigationController pushViewController:detail animated:YES];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void) lotteryListNetWork{
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes =  [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"application/x-json",@"text/html", @"text/plain", nil];
    
    NSMutableDictionary *daDict = [NSMutableDictionary dictionary];
    [daDict setValue:@"b64112d0a44292af" forKey:@"appkey"];
    [daDict setValue:self.cpId forKey:@"caipiaoid"];
    
    
    
    [manager POST:DEF_INTERFACE_HISTORY parameters:daDict progress:^(NSProgress * _Nonnull downloadProgress) {
        
    }
          success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
              
              if ([responseObject[@"status"] integerValue] == 0) {
                  
                  
                  //正确逻辑处理
                  self.lotterylistArrayList = (NSMutableArray *)[MTLJSONAdapter modelsOfClass:[LotteryListModel class] fromJSONArray:responseObject[@"result"][@"list"] error:nil];
                  
                  
                  
                  [self.tableView reloadData];
              }else{
                  //错误逻辑处理
                  [MXUtility showTips:responseObject[@"msg"]];
                  
              }
              [self.tableView.mj_header endRefreshing];
          }
     
          failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull   error) {
              
              NSString *message;
              
              if(error.code == kCFURLErrorNotConnectedToInternet || error.code == kCFURLErrorCannotConnectToHost)
              {
                  message = @"网络不可用，请检查网络连接";
              }else if (error.code == kCFURLErrorTimedOut)
              {
                  message = @"网络请求超时";
              }else
              {
                  message = @"请求失败，请稍后再试";
              }
              
              [MXUtility showTips:message];
              
              CC_LOG_VALUE(error.localizedDescription);
              
              [self.lotterylistArrayList removeAllObjects];
              
              [self.tableView.mj_header endRefreshing];
          }];
    
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
